import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import userImage from "@/components/public/userImage-upload.vue"; //弹框form
import CustomDialog from "@/components/constructionSiteLaboratoryManage/equipmentVerificationCalibration/table/index.vue";

// 
import {
    saveEquipmentTestingInfo, //保存
    getEquipmentTestingInfo, //详情

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",

    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //主键id 有:编辑 无:新增
                companyId: "", //标段id
                companyName: "", //标段名称
                deviceId: "", //设备id
                deviceName:"",
                measuringControl: "", //计量管理方式
                calibration: "", //校准单位
                certificateType: "", //证书类型
                certificateCode: "", //证书编号
                calibrationDates: this.common.day(0), //校准日期
                effectiveDates: this.common.day(0), //有效日期
                calibrationResult: "", //检定校准结果
                remark: "", //备注
                picture: "", //图片
                recordfiles: []
            },
            formObj: formJs.formObj,
            formBcObj: formJs.formBcObj,
            btnShow: true,
            currentDetail: null,
            fileList: [],

            materialRow: {},
            materialShow: false,
        };
    },
    components: {
        publicForm,
        userImage,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });

            this.btnShow = true
            this.formData.picture = "picture" + Date.now();
            this.formObj[8].uploadSelect=true;

            this.formData.companyId = this.userInfo.companyId; //标段id
            this.formData.companyName = this.userInfo.companyFullName; //标段名称
        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 

            this.formObj.map((item) => {
                item.disabled = false;
            });

            this.btnShow = true;
            this.formObj[8].uploadSelect=true;

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[8].uploadSelect=false;
        }
        this.getHeight();
    },
    methods: {
        refreshList() { },
        materialDialog() {
            this.materialShow = false;
        },
        getMateral(data,types){
            console.log(data,types);
            this.formData.deviceName=data.deviceName+"("+data.deviceId+")";
            this.formData.deviceId=data.fId;
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if(a=='deviceName'){
                this.materialShow = true;
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getEquipmentTestingInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpMsEquipmenttesting;
                    this.formData = data
                    this.loading = false;

                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm(status) {

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.formData.typ = this.typs
            this.isLock = true
            saveEquipmentTestingInfo(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else if (res.errcode == -1) {
                        this.loading = false;

                        this.visible = false;
                        this.$message.error(res.data);
                        this.$emit("refreshList", status);
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};